package com.ruoyi.web.controller.modules;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.modules.domain.Contract;
import com.ruoyi.modules.service.ContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 签订合同
 */
@RestController
@RequestMapping("/hsien/contract/data")
public class ContractController extends BaseController
{

    private static final Logger log = LoggerFactory.getLogger(RestController.class);

    @Autowired
    private ContractService contractService;

    /**
     * 查询合同信息
     * @param contract 合同对象
     * @return
     */
    @PreAuthorize("@ss.hasAnyPermi('hsien:signcontract:query,student:signcontract:query')")
    @GetMapping("/list")
    public TableDataInfo list(Contract contract)
    {
        startPage();
        List<Contract> list = contractService.selectContractList(contract);
        return getDataTable(list);
    }

    /**
     * 导出合同数据
     * @param contract 合同对象
     * @return
     */
    @Log(title = "导出合同数据", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Contract contract)
    {
        List<Contract> list = contractService.selectContractList(contract);
        ExcelUtil<Contract> util = new ExcelUtil<Contract>(Contract.class);
        return util.exportExcel(list, "签约合同数据");
    }

    /**
     * 根据合同ID查询合同信息
     * @param contractId 合同编号
     * @return
     */
    @ApiOperation("根据合同ID查询合同信息")
    @GetMapping(value = "/{contractId}")
    public AjaxResult getInfo(@PathVariable Long contractId)
    {
        return AjaxResult.success(contractService.selectContractById(contractId));
    }


    /**
     * 根据用户名查询最新添加的合同信息
     * @return
     */
    @GetMapping("/newOne/{firstName}")
    public AjaxResult selectNewCreateByFirstName(@PathVariable String firstName)
    {
        return AjaxResult.success(contractService.selectNewCreateByFirstName(firstName));
    }


    /**
     * 新增合同信息
     * @param contract 合同对象
     * @return
     */
    @Log(title = "新增合同信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Contract contract)
    {
        contract.setCreateBy(SecurityUtils.getUsername());
        return toAjax(contractService.insertContract(contract));
    }

    /**
     * 修改合同信息
     * @param contract 合同对象
     * @return
     */
    @Log(title = "修改合同信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Contract contract)
    {
        contract.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(contractService.updateContract(contract));
    }


    /**
     * 删除合同信息
     * @param contractIds 合同编号,可批量
     * @return
     */
    @Log(title = "删除合同信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{contractIds}")
    public AjaxResult remove(@PathVariable Long[] contractIds)
    {
        return toAjax(contractService.deleteContractByIds(contractIds));
    }

}
